package com.openapi.protocol.decode.subPackageHandle.mediaData;

import com.openapi.config.JT808ServerConfig;
import com.openapi.protocol.decode.subPackageHandle.SubPackageHandle;
import exsun.jt808.data.util.FtpUtils;
import exsun.jt808.data.util.executorsThread.FixedThreadHandleRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 多媒体数据
 */
public class MediaData_0x0801_Handle {
    private static final Logger logger = LoggerFactory.getLogger(MediaData_0x0801_Handle.class);

    /*
    存储文件至ftp服务器
     */
    public static boolean saveToFTP(JT808ServerConfig jt808ServerConfig, String fileName, String filePath, byte[] data)throws Exception{
//        boolean result = FixedThreadHandleRunnable.ftpSave(jt808ServerConfig.getJt808FtpServer(),
//                jt808ServerConfig.getJt808FtpPort(), jt808ServerConfig.getJt808FtpUserName(),
//                jt808ServerConfig.getJt808FtpPassword(), jt808ServerConfig.getJt808FtpBasePath(),
//                filePath,fileName,data,jt808ServerConfig.getJt808FtpTimeOut());

        FtpUtils.changeWorkingDirectory(jt808ServerConfig.getJt808FtpBasePath(),jt808ServerConfig.getAbsBasePath(),filePath,false);
        return FtpUtils.storageFile(fileName,data,false);
        //return  FtpUtils.changeToRoot(jt808ServerConfig.getJt808FtpBasePath(),filePath,false);
        //return result;
    }

    public static String getFilePath(String phoneNum){
        return "/Media/" + new SimpleDateFormat("yyyy-MM-dd").format(new Date())
                + "/" + phoneNum;
    }

    public static String getFileName(String phoneNum,int channel,int mediaId,int codeType){
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
                +"_"+phoneNum +"_"+channel+"_"+ mediaId+"."+ getFileType(codeType);
    }

    private static String getFileType(int codeType){
        if(codeType==0){
            return "jpeg";
        }
        if(codeType==1){
            return "tif";
        }
        if(codeType==2){
            return "mp3";
        }
        if(codeType==3){
            return "wav";
        }
        if(codeType==3){
            return "wmv";
        }
        return "unKnow";
    }
}


